package com.bush.mall.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import com.bush.mall.common.entity.Result;
import com.bush.mall.common.exception.SaaSException;
import com.bush.mall.member.mapper.MemberReceiveAddressMapper;
import com.bush.mall.member.entity.MemberReceiveAddressEntity;
import com.bush.mall.member.service.MemberReceiveAddressService;

/**
 * 会员收货地址
 * @author RUN
 * @date 2022-11-29
 */
@Service(MemberReceiveAddressService.BEAN_ID)
@Slf4j
public class MemberReceiveAddressServiceImpl extends ServiceImpl<MemberReceiveAddressMapper, MemberReceiveAddressEntity> implements MemberReceiveAddressService {

    /**
     * 查询会员收货地址列表
     * @author RUN
     * @date 2022-11-29
     */
    @Override
    public Result queryForPage(Integer current,Integer size) {
        try{
            Assert.notNull(current,"参数异常");
            Assert.notNull(size,"参数异常");
            Page<MemberReceiveAddressEntity> page = new Page<>(current,size);
            QueryWrapper<MemberReceiveAddressEntity> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("create_time");
            baseMapper.selectPage(page,wrapper);
            return Result.ok(page);
        }catch(Exception e){
            log.error("MemberReceiveAddressService queryForPage ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 创建会员收货地址表单对象
     * @author RUN
     * @date 2022-11-29
     */
    @Override
    public Result saveMemberReceiveAddress(MemberReceiveAddressEntity memberReceiveAddress) {
        try{
            Assert.notNull(memberReceiveAddress,"参数异常");
            boolean flag = this.save(memberReceiveAddress);
            if (flag) return Result.ok("会员收货地址添加成功");
            throw new SaaSException("会员收货地址添加失败");
        }catch(Exception e){
            log.error("MemberReceiveAddressService saveMemberReceiveAddress ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改会员收货地址表单对象
     * @author RUN
     * @date 2022-11-29
     */
    @Override
    public Result updateMemberReceiveAddress(MemberReceiveAddressEntity memberReceiveAddress) {
        try{
            Assert.notNull(memberReceiveAddress,"参数异常");
            boolean flag = this.updateById(memberReceiveAddress);
            if (flag) return Result.ok("会员收货地址修改成功");
            throw new SaaSException("会员收货地址修改失败");
        }catch(Exception e){
            log.error("MemberReceiveAddressService updateMemberReceiveAddress ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 通过id删除会员收货地址表单
     * @author RUN
     * @date 2022-11-29
     */
    @Override
    public Result deleteMemberReceiveAddress(Long id) {
        try{
            Assert.notNull(id,"参数异常");
            boolean flag = this.removeById(id);
            if (flag) return Result.ok("会员收货地址删除成功");
            throw new SaaSException("会员收货地址删除失败");
        }catch(Exception e){
            log.error("MemberReceiveAddressService deleteMemberReceiveAddress ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }




}